/***********************************************************************
	filename: 	DaterUserManager.h
	created:	21/4/2012
	author:		4Ys
	
	purpose:	Implements a base class for user manager
*************************************************************************/

#include "DaterUserManager.h"

namespace Dater
{
	//----------------------------------------------------------------------------//
	template<> UserManager* Singleton<UserManager>::ms_Singleton = 0;

	//----------------------------------------------------------------------------//
	UserManager::UserManager(void)
	{
		//get _uid_counter from database
	}

	//----------------------------------------------------------------------------//
	UserManager::~UserManager(void)
	{
		removeAllUsers();
	}

	//----------------------------------------------------------------------------//
	void UserManager::setCurrentUser(User* user)
	{
		//get data from server
		//now still formal logic
		_currentUser = user;
	}

	User* UserManager::getCurrentUser()
	{
		return _currentUser;
	}

	//----------------------------------------------------------------------------//
	/*User* UserManager::registerUser(const std::string& name, const std::string& Email, const std::string &AccountID,
			const std::string& passWord)
	{
		//formal logic, need server to complete
		return new User(name,1);
	}*/

	//----------------------------------------------------------------------------//
	User* UserManager::addRelatedUser(int uid, const std::string& hypocorism,
			const std::string& gender, const std::string& birthday, const std::string& state)
	{
		if(isUserRelated(uid))
		{
			return getUser(uid);
		}


		User* addedUser = new User(hypocorism, uid, gender, birthday, state);
		_relatedUserRegistry[uid] = addedUser;
		return addedUser;
	}

	//----------------------------------------------------------------------------//
	void UserManager::removeRelatedUser(User* theUser)
	{
		RelatedUserRegistry::iterator upos = _relatedUserRegistry.find(theUser->getUid());
		if (upos != _relatedUserRegistry.end())
		{
			_relatedUserRegistry.erase(upos);
			
			delete theUser;
		}
	}

	//----------------------------------------------------------------------------//
	void UserManager::removeRelatedUser(int uid)
	{
		RelatedUserRegistry::iterator upos = _relatedUserRegistry.find(uid);
		if (upos != _relatedUserRegistry.end())
		{
			User* theUser = upos->second;
			_relatedUserRegistry.erase(upos);
			
			delete theUser;
		}
	}

	//----------------------------------------------------------------------------//
	User* UserManager::getUser(int uid)
	{
		RelatedUserRegistry::const_iterator upos = _relatedUserRegistry.find(uid);

		if (upos == _relatedUserRegistry.end())
		{
			//exception! the event does not exist.
		}

		return upos->second;
	}

	//----------------------------------------------------------------------------//
	bool UserManager::isUserRelated(int uid)
	{
		return (_relatedUserRegistry.find(uid) != _relatedUserRegistry.end());
	}

	//----------------------------------------------------------------------------//
	void UserManager::removeAllUsers(void)
	{
		unsigned long user_uid;
		while (!_relatedUserRegistry.empty())
		{
			user_uid = _relatedUserRegistry.begin()->first;
			removeRelatedUser(user_uid);
		}
	}

	//----------------------------------------------------------------------------//
}